package com.soulgame.sms.pay;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;

import com.soul.sdk.SGProxy;
import com.soul.sdk.constants.ResultCode;
import com.soul.sdk.plugin.pay.IPayCallBack;
import com.soul.sdk.plugin.pay.PayParams;
import com.soul.sdk.utils.SGDebug;
import com.soul.sdk.utils.ToastUtil;
import com.soul.sdk.utils.json.KJSONObject;
import com.soul.sdk.vo.VoSdkServerConfig;
import com.soulgame.sgsdkproject.sgtool.AmountUtils;
import com.soulgame.sgsdkproject.sgtool.DeviceUtil;
import com.soulgame.sms.contant.PayType;
import com.soulgame.sms.pay.dialog.PayDialog;
import com.soulgame.sms.pay.dialog.PayDialog.IPayDialogListener;
import com.soulgame.sms.pay.vo.VoADMPayParams;
import com.soulgame.sms.pay.vo.VoPayParams;
import com.soulgame.sms.sms.SendSmsUtils;
import com.soulgame.sms.sms.SendSmsUtils.ISendSmsListener;
import com.soulgame.sms.util.ResourcesUtil;

/**
 * @author lch
 * @version Create time：2017-3-15
 * @类说明：电信爱动漫
 */
public class ADMPay extends SMSPay {

	private Activity mActivity;

	private ADMPay self;

	private VoADMPayParams mVoADMPayParams;

	private PayDialog mPayDialog;

	private ProgressDialog mProgressDialog = null;

	private static final String SPLIT = "#";

	/**
	 * 是否需要弹二次确认框
	 */
	private boolean isNeedDialog = true;

	@Override
	public String getPayType() {
		return PayType.ADM;
	}

	@Override
	public VoPayParams getVoPayParams() {
		return mVoADMPayParams;
	}

	@Override
	public void init(Activity pActivity, KJSONObject pSdkParamsJSON) {
		self = this;
		this.mActivity = pActivity;
		this.mVoADMPayParams = new VoADMPayParams(pSdkParamsJSON);
	}

	@Override
	public void pay(Activity pActivity, PayParams pPayParams, IPayCallBack pPayCallBack) {

		VoSdkServerConfig pVoServerConfig = SGProxy.getInstance().getVoSdkServerConfig(pActivity);
		// if(pVoServerConfig != null){
		// // 爱动漫2次确认是否开启
		// if(pVoServerConfig.isN2Confirm()){
		// isNeedDialog = true;
		// }else{
		// isNeedDialog = false;
		// }
		// }

		this.isNeedDialog = false;

		if (isNeedDialog) {
			this.showPayDialog(pActivity, pPayParams, pPayCallBack);
		} else {
			this.sendSms(pActivity, pPayParams, pPayCallBack);
		}
		
		//this.showCustomDialog(pActivity, pPayParams, pPayCallBack);
	}

	private ProgressDialog createProcessDialog() {
		mProgressDialog = new ProgressDialog(mActivity);
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDialog.setTitle("提示");
		mProgressDialog.setMessage("正在处理,请稍候...");
		mProgressDialog.setIndeterminate(false);
		mProgressDialog.setCancelable(false);
		return mProgressDialog;
	}

	/**
	 * 爱动漫官方包 用到此 对话框
	 * 
	 * @param pActivity
	 * @param pPayParams
	 * @param pPayCallBack
	 */
	private void showCustomDialog(final Activity pActivity, final PayParams pPayParams, final IPayCallBack pPayCallBack) {
		String amount = pPayParams.getPrice();
		String price = AmountUtils.changeF2Y(amount);
		String message = "您正在购买 " + pPayParams.getProductName() + ", 价格 " + price + "元(不含通信费), 购买成功后获得"
				+ pPayParams.getProductName() + " ,取消不扣费。";
		new AlertDialog.Builder(mActivity).setTitle("提示").setMessage(message).setCancelable(false)
				.setPositiveButton("购买", new OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						arg0.dismiss();
						createProcessDialog().show();
						self.sendSms(pActivity, pPayParams, pPayCallBack);
					}
				}).setNegativeButton("取消", new OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						arg0.dismiss();
						if (pPayCallBack != null) {
							pPayCallBack.onFail(ResultCode.CODE_PAY_CANCEL, "用户点击取消支付", pPayParams);
						}
					}
				}).show();
	}

	private void showPayDialog(final Activity pActivity, final PayParams pPayParams, final IPayCallBack pPayCallBack) {

		int theme = ResourcesUtil.getResource(pActivity, "adm_pay_dialog", "style");
		String amount = pPayParams.getPrice();
		String price = AmountUtils.changeF2Y(amount);

		this.mPayDialog = new PayDialog(pActivity, theme, pPayParams.getProductName(), price, new IPayDialogListener() {

			@Override
			public void payConfirm(int index) {

				self.sendSms(pActivity, pPayParams, pPayCallBack);
			}

			@Override
			public void payCancel() {

				if (pPayCallBack != null) {
					pPayCallBack.onFail(ResultCode.CODE_PAY_CANCEL, "用户点击取消支付", pPayParams);
				}
			}

		});

		mPayDialog.show();

	}

	/**
	 * 发送短信
	 * 
	 * @param pActivity
	 * @param pPayParams
	 * @param pPayCallBack
	 */
	private void sendSms(final Activity pActivity, final PayParams pPayParams, final IPayCallBack pPayCallBack) {
		String pProductId = pPayParams.getProductId();
		String pAddress = mVoADMPayParams.getPhone(pProductId);
		String pPayCode = mVoADMPayParams.getPayCode(pProductId);
		// 爱动漫 SDK 订单号 跟服务端商量好，固定生成 16位
		String pSdkOrderId = pPayParams.getSdkOrderId();
		// 前23位为计费编号，后面为自定义字符，总共不要超过68位
		// AppToken 24位+ SdkOrderId 16位 + SPLIT
		String pSmsContent = pPayCode + SPLIT + DeviceUtil.getAppToken(pActivity).substring(0, 24) + SPLIT + pSdkOrderId
				+ SPLIT;

		if (SGDebug.isDebug()) {
			SGDebug.d(this,
					String.format("pProductId = %s || pAddress = %s || pSmsContent = %s", pProductId, pAddress, pSmsContent));
		}

		SendSmsUtils.sendSms(pActivity, pAddress, pSmsContent, pPayParams, new ISendSmsListener() {

			@Override
			public void onSentResult(int pSendSmsResultCode, Object pObjectParam) {

				// 关闭支付对话框
				self.dimissPayDialog();

				switch (pSendSmsResultCode) {
				case SendSmsUtils.SEND_SMS_RESULT_SUCCESS:
					if (pPayCallBack != null) {
						pPayCallBack.onSuccess(ResultCode.CODE_PAY_SUCCESS, "短信发送成功", pPayParams);
					}

					//ToastUtil.showShort(pActivity, "支付短信发送成功");
					SGDebug.print_d("支付短信发送成功");

					break;
				case SendSmsUtils.SEND_SMS_RESULT_FAIL:

					if (pPayCallBack != null) {
						pPayCallBack.onFail(ResultCode.CODE_PAY_FAIL, "短信发送失败", pPayParams);
					}

					//ToastUtil.showShort(pActivity, "短信发送失败");
					SGDebug.print_w("短信发送失败");

					break;
				case SendSmsUtils.SEND_SMS_RESULT_TIME_OUT:

					if (pPayCallBack != null) {
						pPayCallBack.onFail(ResultCode.CODE_PAY_TIMEOUT, "短信发送超时", pPayParams);
					}

					//ToastUtil.showShort(pActivity, "短信发送超时");
					SGDebug.print_w("短信发送超时");
					break;

				}

			}
		});

	}

	private void dimissPayDialog() {
		if (mPayDialog != null) {
			mPayDialog.dismiss();
		}

		if (mProgressDialog != null) {
			if (mProgressDialog.isShowing()) {
				mProgressDialog.dismiss();
			}
			mProgressDialog = null;
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		SendSmsUtils.onDestroy();
	}

}
